home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
bnce_ra.exe
/
BOUNCE.C
next >
Wrap
C/C++ Source or Header
|
1989-04-04
|
9KB
|
378 lines
/* bounce.c - bouncing ball
by: R. Abramson, CIS ID 74676,3345
Usage: bounce [/b] [/c] [/L] [delay]
where: /b = show border, delay = integer less than 32768, /c = show color
/L = no bottom line legend
Delays needed to slow down display on an AT
EXAMPLE -> bounce /b 8000
kbd controls
0 - top speed
9 - slow speed (8000)
f - speed x2
s - speed /2
b - border on
c - border off
l - kill bottom legend
L - restore bottom legend
1 - expand
2 - shrink
a - add a ball
A - remove a ball
g - shoot gun
Esc - Quit
If you want to recompile:
1. Written in MS QC ver 1.0. Medium model.
2. To compile, requires bounce.c, video.h AND EITHER video.c or video.obj
3. NOTE - needs 8K stack, as in -> link bounce video /stack:8912
*/
#include <memory.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
#include <bios.h>
#include <string.h>
#include "video.h"
#define BOOL int
#define TRUE 1
#define FALSE 0
#define W 80 /* screenwidth */
#define MAXBALLS 8
/* possible object trajectories */
#define Se 1
#define Ne 2
#define Nw 3
#define Sw 4
/* an 'object' */
typedef struct {
int h; /* dimensions */
int w;
int x0; /* last pos */
int y0;
int x; /* current pos */
int y;
int xn; /* next pos */
int yn;
int t; /* trajectory */
int v; /* velocity */
char d[200]; /* pattern data */
} object_t;
static char ball[] = {
(char)219,(char)219,(char)219,(char)219,
(char)219,(char)219,(char)219,(char)219
};
int main( int argc, char **argv );
void o_put( object_t *o, object_t *h, vid_rec *v );
void o_unput( object_t *h, object_t *o, vid_rec *v );
void o_clr( object_t *o, vid_rec *v );
void delay( int t );
void do_botleg( char *botleg, vid_rec *v );
void calc_next( object_t *o );
int main( int argc, char **argv )
{
int i, t = 8000, jx = 1, numballs = 1, j;
object_t o[MAXBALLS], h[MAXBALLS + 1], bullet;
char ch = 0;
vid_rec v0, v;
char scr_buf[4000];
unsigned num;
BOOL color_flag = FALSE, bor_flag = TRUE, bot_flag = TRUE;
static char botleg[] = " Border b/c Add Balls a/A Speed 9/0/f/s Size 1/2 Line l/L Gun g [Esc]";
/* parse command line... */
for ( i = 1; i < argc; i++ ) {
if ( strcmp(argv[i], "/c") == 0 ) color_flag = TRUE;
if ( strcmp(argv[i], "/b") == 0 ) bor_flag = TRUE;
if ( strcmp(argv[i], "/L") == 0 ) bot_flag = FALSE;
if ( 0 != atoi(argv[i]) ) t = atoi( argv[i] );
}
/* init the videos */
v0.mode = get_vid_mode();
v0.page = get_vid_page();
v.page = v0.page;
v.EQUIP_FLAG = (_bios_equiplist() & (unsigned) 0x0030) >> 4;
if ( v.EQUIP_FLAG == 3 ) {
v.VIDEO_SEG = 0xb000; /* MONO, HERC */
v0.VIDEO_SEG = 0xb000;
v.mode = 7; /* if mono, we'll use mode 7 */
}
else {
v.VIDEO_SEG = 0xb800; /* CGA OR ANY OTHER DISPLAY */
v0.VIDEO_SEG = 0xb800;
/* if not mono, we'll use mode 2 or 3 */
if ( color_flag )
v.mode = 3;
else
v.mode = 2;
}
num = crsr_pos_get( v0.page );
v0.crsr_col = (char) (num & 0x00ff);
v0.crsr_row = (char)( num/256 );
scr_save( &v0, scr_buf );
/* clear up our screen */
crsr_pos_hide( v.page );
cl_scr( &v );
if ( bot_flag ) do_botleg( botleg, &v );
if ( bor_flag ) bor_wndw( 0, 0, 23, W-1, &v );
for ( i = 0; i < MAXBALLS; i++ ) {
o[i].h = jx;
o[i].w = 2*jx;
memset( o[i].d, (char) 219, sizeof( o[i].d ) );
o[i].x = 1 + i*(W-1)/MAXBALLS;
o[i].y = 2 + i*23/MAXBALLS;
o[i].t = 1 + (i % 4);
o[i].v = i + 1;
}
bullet.h = 1;
bullet.w = 1;
bullet.x = 38;
bullet.y = 24;
bullet.d[0] = (char) 4;
while ( ch != 27 ) {
if ( kbhit() ) {
ch = (char) getch();
switch ( ch ) {
case 'f':
t /= 2;
break;
case 's':
if ( t == 0 ) t = 1; else t *= 2;
break;
case '0':
t = 0;
break;
case '9':
t = 8000;
break;
case 'b':
bor_wndw( 0, 0, 23, W-1, &v );
bor_flag = TRUE;
break;
case 'c':
bor_flag = FALSE;
cl_scr ( &v );
if ( bot_flag ) do_botleg( botleg, &v );
break;
case 'l':
bot_flag = TRUE;
do_botleg( botleg, &v );
break;
case 'L':
bot_flag = FALSE;
cl_scr ( &v );
if ( bor_flag ) bor_wndw( 0, 0, 23, W-1, &v );
break;
case '1':
if ( jx < 10 ) jx++;
break;
case '2':
if ( jx > 1 ) jx--;
break;
case 'a':
if (numballs < MAXBALLS) {
numballs++;
o[numballs - 1].v = numballs;
}
break;
case 'A':
if (numballs > 1 ) numballs --;
break;
case 'g':
bullet.y = 22;
if ( bullet.x % 6 != 0 ) bullet.x++; else bullet.x-=5;
break;
}
}
for( i = 0; i < numballs; i++ ) {
o[i].h = jx;
o[i].w = 2*jx;
calc_next( &o[i] );
o_put( &o[i], &h[i], &v );
}
if ( bullet.y < 23 && bullet.y > 0 ) {
o_put( &bullet, &h[MAXBALLS], &v );
}
for( i = 0; i < numballs; i++ ) {
if ( (bullet.y>=o[i].y&&bullet.y<=o[i].y+o[i].h)&& \
(bullet.x>=o[i].x&&bullet.x<=o[i].x+o[i].w)) {
printf( "\a" );
o_clr( &o[i], &v );
numballs--;
for ( j = i; j < numballs; j++ ) {
memcpy( &o[j], &o[j+1], sizeof( object_t ) );
}
}
}
delay( t );
for( i = 0; i < numballs; i++ ) {
o_clr( &o[i], &v );
}
if ( bullet.y < 23 && bullet.y > 0 ) {
o_clr( &bullet, &v );
bullet.y--;
}
/*
o_unput( &h, &o, &v );
delay( t );
o_unput( &h2, &o2, &v );
*/
}
/* close up shop */
set_vid_mode( v0.mode );
scr_restore( &v0, scr_buf );
crsr_pos_set( v0.crsr_row, v0.crsr_col, v0.page );
return ch;
}
void o_put( object_t *o, object_t *h, vid_rec *v )
{
char far *p;
char far *q;
int i,j,z;
char t;
h->h = o->h;
h->w = o->w;
p = (char far *) &o->d[0];
q = (char far *) &h->d[0];
for ( i = 0; i < o->h; i++ ) {
for (j = 0; j < o->w; j++ ) {
z = 160*(o->y + i) + 2*(o->x + j);
movedata(v->VIDEO_SEG, z, FP_SEG(q), FP_OFF(q)+i*o->w + j, 1);
t = h->d[i*o->w+j];
t = o->d[i*o->w + j];
if ( t ) {
p = ( char far *) &t;
movedata(FP_SEG(p),FP_OFF(p),v->VIDEO_SEG, z, 1);
}
}
}
}
void o_clr( object_t *o, vid_rec *v )
{
static char a[2] = { 0,7 };
char far *p;
int i,j,z;
p = (char far *) &a[0];
for ( i = 0; i < o->h; i++ ) {
for (j = 0; j < o->w; j++ ) {
z = 160*(o->y + i) + 2*(o->x + j);
movedata(FP_SEG(p),FP_OFF(p),v->VIDEO_SEG, z, 2);
}
}
}
void o_unput( object_t *h, object_t *o, vid_rec *v )
{
char far *p;
int i,j;
char t;
p = (char far *) &h->d[0];
for ( i = 0; i < h->h; i++ ) {
for (j = 0; j < h->w; j++ ) {
t = h->d[i*h->w + j];
p = ( char far *) &t;
movedata(FP_SEG(p),FP_OFF(p),v->VIDEO_SEG,(o->y+i)*160+2*(o->x+j), 1);
}
}
}
void delay ( int t )
{
while ( t )
t--;
}
void do_botleg( char *botleg, vid_rec *v )
{
int k;
char ac[2];
char far *p;
ac[0] = 32;
ac[1] = 'p';
p = &ac[0];
for ( k = 0; k < 80; k++ ) /* clear & rev. video the bot line of display */
movedata( FP_SEG( p ), FP_OFF( p ), v->VIDEO_SEG, 3840 + k*2, 2 );
p = &botleg[0];
for ( k = 0; k < strlen( botleg ); k++ ) /* put up the bot legend */
movedata( FP_SEG( p ), FP_OFF( p ) + k, v->VIDEO_SEG, 3840 + k*2, 1 );
}
void calc_next( object_t *o )
{
switch ( o->t ) {
case Se:
if ( ( o->x<W-o->w-o->v ) && ( o->y<24-o->h-1 ) ) {
o->x+=o->v; o->y++;
}
else if ( ( o->x>=W-o->w-o->v ) && ( o->y>=24-o->h-1 ) ) {
o->t = Nw; o->x-=o->v; o->y--;
}
else if ( o->x>=W-o->w-o->v ) {
o->t = Sw; o->x-=o->v; o->y++;
}
else if ( o->y>=24-o->h-1 ) {
o->t = Ne; o->x+=o->v; o->y--;
}
break;
case Ne:
if ( ( o->x<W-o->w-o->v ) && ( o->y>1 ) ) {
o->x+=o->v; o->y--;
}
else if ( ( o->x>=W-o->w-o->v ) && ( o->y<=1 ) ) {
o->t = Sw; o->x-=o->v; o->y++;
}
else if ( o->x>=W-o->w-o->v ) {
o->t = Nw; o->x-=o->v; o->y--;
}
else if ( o->y<=1 ) {
o->t = Se; o->x+=o->v; o->y++;
}
break;
case Nw:
if ( ( o->x>o->v ) && ( o->y>1 ) ) {
o->x-=o->v; o->y--;